﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using System.Data.Entity;


namespace DAL
{
    //评论管理Dal层
    public class DAL_View_CommentAU
    {
        //查询所有的评论以及条件查询评论
        //分页查询                                                 当前页数     每页几条     文章标题
        public static Pager<View_CommentAU> GetView_CommentAUs(int PageIndex, int PageSize, string title)
        {
            using (var db = new KQCMS())
            {
                //List<Where> where = new List<Where>();
                //总条数  
                var count = db.View_CommentAU.Count() /*% PageSize == 0 ? db.View_CommentAU.Count() / PageSize : db.View_CommentAU.Count() / PageSize + 1*/;
                //所有的数据
                var list = db.View_CommentAU.OrderByDescending(x => x.cmtime).Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
                //起始时间
                //if (!string.IsNullOrEmpty(cmtime)) { 
                //    //count = db.View_CommentAU.Where(x => (x.cmtime >= Convert.ToDateTime(cmtime))).ToList().Count();
                //    list = list.Where(x => x.cmtime >= Convert.ToDateTime(cmtime)).ToList();
                //}
                //文章标题
                if (!string.IsNullOrEmpty(title))
                {
                    count = db.View_CommentAU.Where(x => x.title.Contains(title)).Count();
                    list = list.Where(x => x.title.Contains(title)).ToList();
                }
                Pager<View_CommentAU> p = new Pager<View_CommentAU>();
                p.Count = count;
                p.Data = list;
                return p;
            }
        }
        //添加评论
        public static int AddCMS_Comment(CMS_Comment cm)
        {
            using (var db = new KQCMS())
            {
                cm.cmtime = DateTime.Now;
                db.Entry(cm).State = EntityState.Added;

                if (db.SaveChanges() > 0)
                    return 1;
                else
                    return 0;
            }
        }
        //根据cmid查询数据 
        //public static List<View_CommentAU> SelectID(string cmid)
        //{
        //    using (var db = new KQCMS()) {
        //        var list = db.View_CommentAU.Where(x => x.aid == Convert.ToInt32(cmid)).ToList();
        //        return list;
        //    }
        //}
        //根据传来的对象进行修改
        //public static int UpdateView_CommentAU(CMS_Comment cm)
        //{
        //    using (var db = new KQCMS())
        //    { 
        //        db.Entry(cm).State = EntityState.Modified;
        //        if (db.SaveChanges() > 0)
        //            return 1;
        //        else
        //            return 0;
        //    }
        //}

        //根据id删除评论
        public static int DeleteID(int cmid) {
            using (var db = new KQCMS()) {
                var list = db.CMS_Comment.Where(x => x.cmid == cmid).FirstOrDefault();  
                db.Entry(list).State = EntityState.Deleted;
                if (db.SaveChanges() > 0)
                    return 1;
                else
                    return 0;
            }
        }
    }
}
